package com.jk.smartelderlycare.controller;

import com.jk.smartelderlycare.dto.SysPlay;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.security.LoginUser;
import com.jk.smartelderlycare.service.SysPlayServiceFeign;
import com.jk.smartelderlycare.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysPlayController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/15 13:34
 */
@Api(tags = "活动管理模块")
@RestController
@CrossOrigin
@RequestMapping(value = "sysPlay")
public class SysPlayController {
    @Autowired
    private SysPlayServiceFeign sysPlayServiceFeign;

    /**
     * @description: 后端分页查询
     * @param: []
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysPlay>>
     * @author hhk
     * @date: 2024/11/15 13:36
     */
    @ApiOperation(value = "活动后端分页查询",httpMethod = "POST",notes = "活动后端分页查询")
    @PostMapping(value = "findSysPlayPaginationList")
    private ResponseResult<PagedResponse<SysPlay>>findSysPlayPaginationList(@RequestBody SysPlay sysPlay ,@RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize){
        return new ResponseResult<>(200,"查询成功",sysPlayServiceFeign.findSysPlayPaginationList(sysPlay,pageNum,pageSize));
    }
    /**
     * @description: 新增活动
     * @param: [sysPlay]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 13:40
     */
    @ApiOperation(value = "新增活动",httpMethod = "POST",notes = "新增活动")
    @PostMapping(value = "addSysPlay")
    private ResponseResult addSysPlay(@RequestBody SysPlay sysPlay){
        try {
            sysPlay.setPlayDate(new Date());
            sysPlay.setEndDate(new Date());
            sysPlay.setStatus("进行中");
            sysPlayServiceFeign.addSysPlay(sysPlay);
            return new ResponseResult(200,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"新增失败");
        }
    }
    /**
     * @description: 根据id删除
     * @param: [playId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 13:41
     */
    @ApiOperation(value = "根据id删除活动",httpMethod = "DELETE",notes = "根据id删除活动")
    @DeleteMapping(value = "deleteSysPlayById")
    private ResponseResult deleteSysPlayById(@RequestParam Long playId){
        try {
            sysPlayServiceFeign.deleteSysPlayById(playId);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"删除失败");
        }
    }
    /**
     * @description: 根据id查询活动
     * @param: [playId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.dto.SysPlay>
     * @author hhk
     * @date: 2024/11/15 13:43
     */

    @ApiOperation(value = "根据id查询活动",httpMethod = "GET",notes = "根据id查询活动")
    @GetMapping(value = "findSysPlayById")
    private ResponseResult<SysPlay> findSysPlayById(@RequestParam Long playId){
        return new ResponseResult(200,"查询成功",sysPlayServiceFeign.findSysPlayById(playId));
    }
    /**
     * @description: 修改活动
     * @param: [sysPlay]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 13:43
     */
    @ApiOperation(value = "修改活动",httpMethod = "PUT",notes = "修改活动")
    @PutMapping(value = "updateSysPlay")
    private ResponseResult updateSysPlay(@RequestBody SysPlay sysPlay){
        try {
            LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            sysPlay.setModifyDate(new Date());
            sysPlay.setModifyUser(loginUser.getUser().getUserName());
            sysPlayServiceFeign.updateSysPlay(sysPlay);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }
    /**
     * @description: 批量删除活动
     * @param: [ids]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 13:48
     */
    @ApiOperation(value = "批量删除活动",httpMethod = "DELETE",notes = "批量删除活动")
    @DeleteMapping(value = "deleteSysPlayByIds")
    private ResponseResult deleteSysPlayByIds(@RequestParam List<Long> ids){
        try {
            sysPlayServiceFeign.deleteSysPlayByIds(ids);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"删除失败");
        }
    }

    /**
     * @description: 上传图片
     * @param: [file, request]
     * @return: java.lang.String
     * @author hhk
     * @date: 2024/11/7 10:54
     */
    @ApiOperation(value = "上传图片",httpMethod = "POST",notes = "上传图片")
    @PostMapping("uploadImg")
    public String uploadImg(MultipartFile file, HttpServletRequest request) {
        return FileUtil.uploadFile(file,request);
    }









}
